package game;

import javax.swing.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * @Author: liwan
 * @Description:
 * @Date: create in 2021/9/2 15:43:48
 */
public class GameFrame extends JFrame {

    public static final int W = 1760;
    public static final int H = 700;

    public GameFrame() {
        this.setTitle("斗战三国");
        this.setSize(this.W, this.H);
        this.setLocationRelativeTo(null);   // 屏幕中心
        this.setResizable(false);   // 不允许拉伸
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        GameFrame gameFrame = new GameFrame();
        GamePanel gamePanel = new GamePanel();
        gameFrame.add(gamePanel);
        gameFrame.createKeyLister(gamePanel);
        gameFrame.setVisible(true);
    }

    /**
     * @Author: liwan
     * @Description: 键盘监听
     * @Params:
     * @Return:
     * @DateTime: 2021/9/2 16:20:11
     */
    public void createKeyLister(GamePanel gamePanel) {
        KeyAdapter keyAdapter = new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                new Thread(()->{
                    int code = e.getKeyCode();
                    if(code == 81){
                        gamePanel.setBackIndex((gamePanel.getBackIndex() + 1) % gamePanel.getBackNum());
                        gamePanel.repaint();
                    } else if (code == 68) {
                        gamePanel.getHero().setRight(true);
                        gamePanel.getHero().rightMove();
                    } else if (code == 87) {
                        gamePanel.getHero().topMove();
                    } else if (code == 83) {
                        gamePanel.getHero().middleMove();
                    } else if(code == 65){
                        gamePanel.getHero().setRight(false);
                        gamePanel.getHero().leftMove();
                    } else if (code == 32) {
                        if(gamePanel.getHeroBule().getW() >= 300){
                            gamePanel.getHero().skill();
                            gamePanel.getHero().isSkill(gamePanel.getEnemy());
                            gamePanel.getHeroBule().setW(gamePanel.getHeroBule().getW() - 300);
                        }
                    } else if (code == 74) {
                        gamePanel.getHero().attack();
                        gamePanel.getHero().isattack(gamePanel.getEnemy());
                    }
                }).start();
            }

            @Override
            public void keyReleased(KeyEvent e) {
                gamePanel.getHero().stopMove();
            }
        };
        this.addKeyListener(keyAdapter);
    }

}
